#include "sharefile.h"
#include "ui_sharefile.h"
#include "index.h"
#include "client.h"

ShareFile::ShareFile(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShareFile)
{
    ui->setupUi(this);
}

ShareFile::~ShareFile()
{
    delete ui;
}

void ShareFile::updateFriend_LW()
{
    ui->friend_LW->clear();
    ui->friend_LW->addItems(Index::getInstance().getFriend()->m_FriendList);
}

void ShareFile::on_allSelect_PB_clicked()//全选槽函数
{
    for(int i = 0; i < ui->friend_LW->count(); i++){
        ui->friend_LW->item(i)->setSelected(true);
    }
}

void ShareFile::on_cancelallSelect_PB_clicked()//取消全选槽函数
{
    for(int i = 0; i < ui->friend_LW->count(); i++){
        ui->friend_LW->item(i)->setSelected(false);
    }
}

void ShareFile::on_sure_PB_clicked()
{
    //拼接分享文件的完整路径
    QString strCurPath = Index::getInstance().getFile()->m_strCurPath;
    QString m_strShareFileName = Index::getInstance().getFile()->m_strShareFileName;
    QString strPath = strCurPath + "/" + m_strShareFileName;
    //获取选择的好友和好友数量
    QList<QListWidgetItem*> pItems = ui->friend_LW->selectedItems();
    int friendNum = pItems.size();
    //构建PDU
    PDU* pdu = mkPDU(friendNum* 32+strPath.size()+1);
    pdu->uiMsgType = ENUM_MSG_TYPE_SHARE_FILE_REQUEST;
    //当前用户名和好友数量放入caData
    memcpy(pdu->caData, Client::getInstance().m_strLoginName.toStdString().c_str(), 32);
    memcpy(pdu->caData +32, &friendNum, sizeof(int));
    //选择的好友名和完整路径放入caMsg
    for(int i=0; i<friendNum; i++){
        memcpy(pdu->caMsg+32*i, pItems.at(i)->text().toStdString().c_str(), 32);
    }
    memcpy(pdu->caMsg+32*friendNum, strPath.toStdString().c_str(), strPath.size());
    //发送
    Client::getInstance().sendPDU(pdu);
}
